// RemoveBookMark.cpp : implementation file
//

#include "stdafx.h"
#include "SwordBible.h"
#include "RemoveBookMark.h"
#include "SwordBibleDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRemoveBookMark dialog


CRemoveBookMark::CRemoveBookMark(CWnd* pParent /*=NULL*/)
	: CDialog(CRemoveBookMark::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRemoveBookMark)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CRemoveBookMark::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRemoveBookMark)
	DDX_Control(pDX, IDC_LIST_BOOKMARK, m_removeSelected);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRemoveBookMark, CDialog)
	//{{AFX_MSG_MAP(CRemoveBookMark)
	ON_BN_CLICKED(IDC_BOOKMARK_OK, OnBookmarkOk)
	ON_BN_CLICKED(IDC_BOOKMARK_CANCEL, OnBookmarkCancel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRemoveBookMark message handlers

BOOL CRemoveBookMark::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	for(int add = 0; add < MAXBOOKMARKS; add++) {
	
		if(CSwordBibleDoc::BookMarks[add] != "")
	m_removeSelected.AddString(CSwordBibleDoc::BookMarks[add]);
	}

	m_removeSelected.SetCurSel(0);

	if( m_removeSelected.GetCount() <= 0)
		this->EndDialog(TRUE);

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CRemoveBookMark::OnBookmarkOk() 
{
	// TODO: Add your control notification handler code here

		if( m_removeSelected.GetCount() <= 0)
		{ this->EndDialog(TRUE); return; }


	CString toRemove;
	m_removeSelected.GetText( m_removeSelected.GetCurSel() , toRemove);

	bool removed = FALSE;
	
	for(int i =0; i < MAXBOOKMARKS; i++)
		if(CSwordBibleDoc::BookMarks[i] == toRemove)
		{
			
			CSwordBibleDoc::BookMarks[i] = "";
			removed = TRUE;
		}

	if(removed == FALSE) return;


	CMenu *pMainMenu = AfxGetMainWnd()->GetMenu();

	CMenu *pSubMenu = pMainMenu->GetSubMenu(6); // Bookmark is submenu 7


	int numSubMenuEntries = pSubMenu ->GetMenuItemCount();

	for(int i = 3; i < numSubMenuEntries; i++)
    {
		CString subMenuChild;
		pSubMenu->GetMenuString(i,subMenuChild,MF_BYPOSITION);

		if(subMenuChild == toRemove)
		pSubMenu->RemoveMenu(i,MF_BYPOSITION);


	}





	int index = m_removeSelected.FindString(-1,toRemove);

	m_removeSelected.DeleteString(index);

	m_removeSelected.SetCurSel(0);

	
	
	for( int i = 0; i < MAXBOOKMARKS; i++)
	{

		CString section;
		section.Format("BM%d",i);
	
   CString bookmarkStr = AfxGetApp()->GetProfileString("BOOKMARKS",section , "");
		
		if(bookmarkStr.CompareNoCase(toRemove) == 0)		
	AfxGetApp()->WriteProfileString("BOOKMARKS",section,"");
		
	}
	


	// make sure the index of all bookmarks are correct


	numSubMenuEntries = pSubMenu ->GetMenuItemCount();

	while(numSubMenuEntries > 3)
	{
		CString string;
		pSubMenu->GetMenuString(numSubMenuEntries -1,string,MF_BYPOSITION);

		pSubMenu->ModifyMenu(numSubMenuEntries - 1,MF_BYPOSITION,
			ID_BOOKMARK_STRING + numSubMenuEntries - 1,string);

		numSubMenuEntries--;
	}



	
}

void CRemoveBookMark::OnBookmarkCancel() 
{
	// TODO: Add your control notification handler code here

	OnCancel();
	
}
